package com.apes.framework.plugin.form;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.Connector;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.plugin.filereader.xml.ApesXmlParser;
import com.apes.framework.plugin.form.parser.matcher.AttrsMatcher;
import com.apes.framework.plugin.form.parser.node.FormNodeParser;
import com.apes.framework.plugin.log.timer.Timer;
import com.apes.framework.query.lib.PubToolUtil;
import com.apestech.api.xml.esb.modul.EsbForm;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 功能：表单配置解析器
 *
 * @author xul
 * @create 2018-04-16 11:27
 */
@Service("formService")
public class FormService extends DomainService {
    protected final Logger logger = LogManager.getLogger(FormService.class);

    @Autowired
    Connector connector;

    @Timer("表单解析")
    public Map parse(SimpleRequest request) throws Exception {
        String xml = request.get("content");
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        //创建SAXReader对象
        SAXReader reader = new SAXReader();
        //转换成Document
//        Document document = reader.read(is);

        Document document = reader.read(new File("D:/Users/xul/Desktop/odoo.xml"));

        List<ApesElement> elements = SpringManager.getBean(ApesXmlParser.class).parse(document);
        ApesElement element = elements.stream().findFirst().get();
        if (element != null) return element.parse();
        return null;
    }

    public Map getForm(SimpleRequest request) throws Exception {
        String id = request.get("id");
        return getForm(id);
    }

    public Map getSearchForm(SimpleRequest request) throws Exception {
        String id = request.get("id");
        if(id.indexOf("strategy.")==0 && !"strategy.queryImpTab".equals(id) && !"strategy.queryStopTab".equals(id) && !"strategy.queryTab".equals(id)){  //&& !"strategy.queryTab".equals(id)
            EsbForm form1=new EsbForm();
            form1.setSession(PubToolUtil.getSession(com.apestech.framework.util.DBUtil.getSysSingleDBName("ESB"), connector.getConnection()));
            Object body=request.getData();
            com.apestech.framework.json.JSONObject input=com.apestech.framework.json.JSON.toJSON(body);
            com.apestech.framework.json.JSONObject j=form1.getSearchForm(id, input);
            Map f=j.getInnerMap();
            return f;
        }
        Map form = getForm(id);
        if (!form.containsKey("noSum")) {
            form.put("noSum", "true");
        }
        String key = "search";
        if (form.containsKey(key)) {
            Object o = form.get(key);
            form.remove(key);
            form.put("conditionField", o);
        }
        key = "table";
        if(form.containsKey("table")){
            Map table = (Map) form.get("table");
            form.remove(key);
            if (table.containsKey("widgets")){
               JSONObject json = (JSONObject) JSON.parse(table.get("widgets").toString());
               form.putAll((Map) json);
            }
            form.putAll((Map) table.get("templateOptions"));
        }
        return form;
    }

    private Map getForm(String id) {
        Map form = FormNodeParser.getForm(id);
        if (form == null) throw new RuntimeException("表单： " + id + " 没有配置！");
        return form;
    }

    public Map parseAttrs(SimpleRequest request) throws Exception {
        return new AttrsMatcher().parse(request.get("content"));
    }

}
